<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>01-props基础使用</title>
  </head>
  <body>
    <div id="app"></div>

    <!-- 导入 react 主库 -->
    <script src="../js/react.development.js"></script>
    <!-- 导入 reactDom 扩展库 -->
    <script src="../js/react-dom.development.js"></script>
    <!-- 导入 babel 用于将jsx转为js -->
    <script src="../js/babel.min.js"></script>
    <!-- 导入 prop-types 用于对组件标签属性进行限制 -->
    <script src="../js//prop-types.js"></script>

    <script type="text/babel">
      // 定义类组件
      class Person extends React.Component {
       
        constructor(props) {
            super(props)
            console.log(this.props);   // 构造器如果不接或者不传props此时构造器中实例对象身上的props就拿不到  为 undefined
        }

        static propTypes = {
          name: PropTypes.string.isRequired, // 限制name为string类型并且必传
          sex: PropTypes.string,
          age: PropTypes.number
        };

        static defaultProps = {
          sex: '男女'
        }

        render() {
          console.log(this);
          const { name, age, sex } = this.props;
          return (
            <ul>
              <li>名字：{name}</li>
              <li>年龄：{age}</li>
              <li>性别：{sex}</li>
            </ul>
          );
        }
      }

      // 渲染虚拟DOM
      const p = { name: "st", age: 18, sex: "男" };
      ReactDOM.render(<Person {...p} />, document.getElementById("app"));
    </script>
  </body>
</html>
